# -*- coding: utf-8 -*-

import urllib2, re, tweepy
from passwd import stockhoge as p

consumer_key        = p.consumer_key
consumer_secret     = p.consumer_secret
access_token        = p.access_token
access_token_secret = p.access_token_secret

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth_handler=auth)

try:
    buffer = urllib2.urlopen( 'http://stocks.finance.yahoo.co.jp/stocks/detail/?code=9501.t' ).read( )
except urllib2.HTTPError, e:
    exit( )

time       = re.search( '<td width="1%" valign="middle" nowrap class="yjSt">.*([0-9]{2}:[0-9]{2}).*</strong>', buffer ).group( 1 )
difference = re.search( '<strong class="(?:red|green)Fin">([+-][0-9]+)</strong>', buffer ).group( 1 )
value      = re.search( '<span class="yjFL">([0-9]+)</span>', buffer ).group( 1 )

result = value + '(' + difference + ')[' + time + ']'

api.update_status(result)

